﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RTS;
using RTS.Model;

    using System.Xml.Linq;

    namespace BrickInvaders
    {
        namespace Model
        {
            public static class XMLSoundListParser
            {
                public string fileName { get; set; }
                private XDocument xdoc;
                private List<Sound> _sounds;
                
                public XMLSoundListParser(string filename)
                {
                    try
                    {
                        this.xdoc = XDocument.Load(fileName);
                        _sounds = this.ParseScores();
                    }
                    catch (Exception ex)
                    {
                        this.xdoc = new XDocument(new XElement("SoundsList"));
                        _sounds = new List<Sound>();
                        this.xdoc.Save(fileName);
                    }

                }

                private List<Sound> ParseScores()
                {
                    XElement root = this.xdoc.Root;
                    IEnumerable<XElement> list = root.Descendants(XName.Get("SoundsList"));
                    List<Sound> scores = new List<Sound>();
                    foreach (XElement item in list)
                    {
                        string Name = item.Element("Name").Value;

                        //scores.Add(new Sound(new Player(Name), int.Parse(item.Element("Value").Value), (GameMode)Activator.CreateInstance(Type.GetType(item.Element("Mode").Value))));
                    }
                    return _sounds;
                }

                public List<Sound> GetSounds()
                {
                    return _sounds;
                }

                public void StoreScores(List<Sound> sounds)
                {
                    this._sounds = _sounds;
                    //this._sounds.Sort((x, y) => y.CompareTo(x));
                   // XElement[] Xscores = new XElement[Scores.Count];
                  //  int i = 0;
                   // foreach (Score sc in Scores)
                    {
//Xscores[i++] = new XElement("Score", new XElement("Name", sc.Player.Pseudo), new XElement("Value", sc.Value), new XElement("Mode", sc.Mode.GetType().FullName));
                    }
                  //  this.xdoc = new XDocument(new XElement("Scores", Xscores));

                   // this.xdoc.Save(FILENAME);
                }
                
                public XMLSoundListParser()
                {

                    
                }
            }
        }
    }


